package pailaidai.hc.com.mysvgdemo.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Administrator on 2018/4/12.
 */

public class CornerLayout extends ViewGroup {
    public CornerLayout(Context context) {
        this(context, null);
    }

    public CornerLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CornerLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 定位子组件
     */
    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        for (int a = 0; a < getChildCount(); a++) {
            View view = getChildAt(a);
            if (a == 0) {
                view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
            } else if (a == 1) {
                view.layout(getMeasuredWidth() - view.getMeasuredWidth(), 0, getMeasuredWidth(), view.getMeasuredHeight());
            } else if (a == 2) {
                view.layout(0, getMeasuredHeight() - view.getMeasuredWidth(), view.getMeasuredWidth(), getMeasuredHeight());
            } else if (a == 3) {
                view.layout(getMeasuredWidth() - view.getMeasuredWidth(), getMeasuredHeight() - view.getMeasuredWidth(), getMeasuredWidth(), getMeasuredHeight());
            }
        }
    }

    /**
     * 测量尺寸
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //先测量所有子组件的大小
        this.measureChildren(widthMeasureSpec, heightMeasureSpec);
        //在测量自己的大小
        int with=measureWith(widthMeasureSpec);
        int height=measureHeight(heightMeasureSpec);
        setMeasuredDimension(with,height);

    }

    /**
     * 测量容器的宽度
     */
    private int measureWith(int widthMeasureSpec) {
        int mode = MeasureSpec.getMode(widthMeasureSpec);
        int size = MeasureSpec.getSize(widthMeasureSpec);
        int width = 0;
        if (mode == MeasureSpec.EXACTLY) {
            //match_parent或具体值
            width = size;
        } else if (mode == MeasureSpec.AT_MOST) {
            //wrap_content
            int aWidth = 0;
            int bWidth = 0;
            int cWidth = 0;
            int dWidth = 0;
            for (int i = 0; i < this.getChildCount(); i++) {
                if (i == 0) {
                    aWidth = getChildAt(i).getMeasuredWidth();
                } else if (i == 1) {
                    bWidth = getChildAt(i).getMeasuredWidth();
                } else if (i == 2) {
                    cWidth = getChildAt(i).getMeasuredWidth();
                } else if (i == 3) {
                    dWidth = getChildAt(i).getMeasuredWidth();
                }
            }
            width = Math.max(aWidth,cWidth) + Math.max(bWidth, dWidth);
        }
        return width;
    }

    /**
     * 测量高度
     */
    private int measureHeight(int heightMeasureSpce) {
        int mode = MeasureSpec.getMode(heightMeasureSpce);
        int size = MeasureSpec.getSize(heightMeasureSpce);
        int height=0;
        if(mode==MeasureSpec.EXACTLY){
            height=size;
        }else if(mode==MeasureSpec.AT_MOST){
            int aheight=0;
            int bheight=0;
            int cheight=0;
            int dheight=0;
            for(int i=0;i<getChildCount();i++){
                if(i==0){
                    aheight=getChildAt(i).getMeasuredHeight();
                }else if(i==1){
                    bheight=getChildAt(i).getMeasuredHeight();
                }else if(i==2){
                    cheight=getChildAt(i).getMeasuredHeight();
                }else if(i==3){
                    dheight=getChildAt(i).getMeasuredHeight();
                }
            }
            height=Math.max(aheight,bheight)+Math.max(cheight,dheight);
        }
       return height;
    }

    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return super.generateDefaultLayoutParams();
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return super.generateLayoutParams(attrs);
    }

    @Override
    protected LayoutParams generateLayoutParams(LayoutParams p) {
        return super.generateLayoutParams(p);
    }
}
